package ocp

import (
    `database/sql`
)

// 组成

type rowConverter struct {
    // 行转换
}

type LoadPerson struct {
    // compose the row converter into this loader
    // /将行转换器组成此加载器
    rowConverter
}

func (d *rowConverter) populate(in *Person, scan func(dest ...interface{}) error) error {
    return scan(in.Name, in.Email)

}

func (loader *LoadPerson) ByID(id int) (Person, error) {
    row := loader.loadFromDB(id)

    person := Person{}
    // call the composed "abstract class"
    err := loader.populate(&person, row.Scan)

    return person, err

}

func (loder *LoadPerson) loadFromDB(id int) *sql.Row {
    // todo : implement

    return nil

}

type LoadAll struct {
    // compose the row converter into this loader
    rowConverter
}

func (loder *LoadPerson) All() ([]Person, error) {
    rows := loder.loadAllFromDB()
    defer rows.Close()

    var output []Person

    for rows.Next() {
        person := Person{}
        // call the composed "abstract class"
        err := loder.populate(&person, rows.Scan)
        if err != nil {
            return nil, err
        }
    }
    return output, nil
}

func (loder *LoadPerson) loadAllFromDB() *sql.Rows {
    // todo : implement

    return nil

}
